package com.mjk.common.ai.service;


import com.mjk.common.ai.base.ThreadContext;
import com.mjk.common.ai.dto.DrawCommentDto;
import com.mjk.common.ai.entity.Draw;
import com.mjk.common.ai.entity.DrawComment;
import com.mjk.common.ai.entity.User;
import com.mjk.common.ai.exception.BaseException;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeComplexQueryWrapper;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.core.redis.datatype.RedisUtils;
import com.mjk.common.util.RandomUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;

import static com.mjk.common.ai.cosntant.RedisKeyConstant.DRAW_COMMENT_LIMIT_KEY;
import static com.mjk.common.ai.enums.ErrorEnum.A_DATA_NOT_FOUND;
import static com.mjk.common.ai.enums.ErrorEnum.A_OPT_TOO_FREQUENTLY;

@Slf4j
@Service
public class DrawCommentService {

    @Autowired
    private BeeDBService beeDBService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 同一用户，5秒内只能提交一次评论
     *
     * @param user   用户
     * @param draw   绘图
     * @param remark 评论内容
     * @return 评论
     */
    public DrawCommentDto add(User user, Draw draw, String remark) {

        String redisKey = MessageFormat.format(DRAW_COMMENT_LIMIT_KEY, user.getId());

        if (!RedisUtils.lock(redisTemplate, redisKey, RandomUtil.getShortUuid(), 5)) {
            throw new BaseException(A_OPT_TOO_FREQUENTLY);
        }
        String uuid = RandomUtil.getShortUuid();
        DrawComment newObj = new DrawComment();
        newObj.setUuid(uuid);
        newObj.setUserId(user.getId());
        newObj.setDrawId(draw.getId());
        newObj.setRemark(remark);
        beeDBService.insert(newObj);

        return DrawCommentDto.builder()
                .uuid(uuid)
                .drawUuid(draw.getUuid())
                .userUuid(user.getUuid())
                .userName(user.getName())
                .remark(remark)
                .createTime(newObj.getCreateTime())
                .build();
    }

    public BeePage<DrawCommentDto> listByPage(long drawId, int currentPage, int pageSize) {
        BeeComplexQueryWrapper wrapper = new BeeComplexQueryWrapper(DrawComment.class, "a");
        wrapper.innerJoin(User.class, "b").on(DrawComment::getUserId, User::getId);
        wrapper.innerJoin(Draw.class, "c").on(DrawComment::getDrawId, Draw::getId);
        wrapper.eq(DrawComment::getDrawId, drawId).eq(DrawComment::getIsDeleted, false);
        wrapper.setPage(currentPage, pageSize);
        return beeDBService.findManyByWrapperWithPage(wrapper);
    }

    public boolean softDel(Long id) {
        BeeQueryWrapper wrapper = new BeeQueryWrapper(DrawComment.class);
        DrawComment drawComment;
        if (Boolean.TRUE.equals(ThreadContext.getCurrentUser().getIsAdmin())) {
            drawComment = wrapper
                    .eq(DrawComment::getId, id)
                    .eq(DrawComment::getIsDeleted, false).one();
        } else {
            drawComment = wrapper
                    .eq(DrawComment::getId, id)
                    .eq(DrawComment::getUserId, ThreadContext.getCurrentUserId())
                    .eq(DrawComment::getIsDeleted, false).one();
        }
        drawComment.setIsDeleted(true);
        wrapper = new BeeQueryWrapper(drawComment);
        wrapper.eq(DrawComment::getId, drawComment.getId());

        return beeDBService.update(wrapper) > 0;
    }

}
